package com.woniuxy.user.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.woniuxy.common.entity.Result;
import com.woniuxy.entity.AllUser;
import com.woniuxy.entity.DTO.LoginDTO;
import com.woniuxy.entity.DTO.PhoneLoginDTO;
import com.woniuxy.user.service.UserService;
import com.woniuxy.util.ResponseData;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("auth")
public class UserController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserService userService;
    //登录接口,用户名密码登录
    @PostMapping("/login")
    public Result<AllUser> login(@RequestBody LoginDTO loginDTO,
                                 HttpSession  httpSession) {
        AllUser login = userService.login(loginDTO.getUsername(), loginDTO.getPassword());
        if(login == null){
            return Result.fail("用户名或密码错误");
        }
        httpSession.setAttribute("loginUser",login);
        //设置过期时间
        httpSession.setMaxInactiveInterval(3600);
        return Result.success(login);
    }
    //获取验证码
    @PostMapping("/getCode")
    public Result<String> getCode(@RequestParam("phone") String phone) {
        //查询电话号码是否存在，存在就发生验证码。
//        String phonetaext =phone.get("phone").asText();
        AllUser alluser = userService.getCode(phone);
        if(alluser != null){
            System.out.println("发送了");
            return Result.success();
        }else {
            return Result.fail("手机号不存在");
        }
    }
    //校验验证码,登录
    @PostMapping("/resetPassword")
    public ResponseData<AllUser> checkCode(@RequestBody PhoneLoginDTO phoneLoginDTO,HttpSession  httpSession) {
        String storedCode = userService.checkCode(phoneLoginDTO.getPhone(), phoneLoginDTO.getCode());
        AllUser alluser = userService.findByPhone(phoneLoginDTO.getPhone());
        if (storedCode == null) {
            return ResponseData.fail(2001,"验证码已过期");
        }else if (!storedCode.equals(phoneLoginDTO.getCode())) {
            return ResponseData.fail(2001,"验证码错误");
        }else if (alluser == null) {
            return ResponseData.fail(2001,"手机号不存在");
        } else {
            httpSession.setAttribute("loginUser",alluser);
            //设置过期时间
            httpSession.setMaxInactiveInterval(3600);
            //删除验证码
            redisTemplate.delete("code:" + phoneLoginDTO.getPhone());
            return ResponseData.ok(alluser);
        }
    }

    /**
     * 获取用户名
     *
     * @param httpSession HttpSession
     * @return String
     */
    @GetMapping("/getUsername")
    public Result<String> getUsername(HttpSession httpSession) {
        return Result.success((String) httpSession.getAttribute("currentUser"));
    }
    //注册
    @PostMapping("/register")
    public Result<String> register(@RequestBody AllUser allUser) {
        //判断用户名是否重复
        AllUser byPhone = userService.findByPhone(allUser.getPhone());
        if(byPhone != null){
            return Result.fail("电话已存在");
        }
        //判断电话号码是否重复
        AllUser byUsername = userService.findByUsername(allUser.getUsername());
        if(byUsername != null){
            return Result.fail("用户名已存在");
        }
        //添加用户
        userService.addUser(allUser);
        return Result.success("注册成功");
    }

}
